package edu.cs.usask.ca.silver.businesslogic.ids;

import java.util.Date;
import java.util.UUID;

import edu.cs.usask.ca.silver.api.persistence.StorageI;
import edu.cs.usask.ca.silver.businesslogic.commands.generic.Group;
import edu.cs.usask.ca.silver.exceptions.SilverBusinessLogicErrorException;

/**
 * This class represents a group id type.
 * 
 * @author Yudi Xue
 * 
 */
public class GroupID extends BaseId {

	public GroupID(UUID id) throws SilverBusinessLogicErrorException {
		super(id);
	}

	public String toString() {
		return "group" + uIdVal.toString();
	}

	public UUID getUUIdVal() {
		return uIdVal;
	}

	@Override
	public boolean equals(BaseId id) {
		return ((id.getClass() == this.getClass()) && (((GroupID) id)
				.getUUIdVal().compareTo(this.getUUIdVal()) == 0));
	}

	@Override
	public String getEntryNameFromDatabase(StorageI storage) throws Exception {
		Group borrowedObj = ((Group) Group.getObjectPoolFactory()
				.borrowObject());
		String name = borrowedObj.getName(this, storage);
		Group.getObjectPoolFactory().returnObject(borrowedObj);
		return name;
	}

	@Override
	public BaseId[] getChildrenObjectIds(StorageI storage) throws Exception {
		Group borrowedObj = ((Group) Group.getObjectPoolFactory()
				.borrowObject());
		BaseId[] result = borrowedObj.getContainedModelProjectIDs(this, storage);
		Group.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public boolean isEntity() {
		return true;
	}

	@Override
	public BaseId getParentObjectId(StorageI storage) throws Exception {
		Group borrowedObj = ((Group) Group.getObjectPoolFactory()
				.borrowObject());
		UserID ownerId = borrowedObj.getOwnerUserID(this, storage);
		Group.getObjectPoolFactory().returnObject(borrowedObj);
		return ownerId;
	}

	@Override
	public boolean hasChildren() {
		return true;
	}

	public void accept(SilverIDVisitor v) throws Exception {
		v.visit(this);
	}

	@Override
	public String getCreatorNameFromDatabase(StorageI storage) throws Exception {
		Group borrowedObj = ((Group) Group.getObjectPoolFactory()
				.borrowObject());
		String creator = borrowedObj.getOwnerUserName(this, storage);
		Group.getObjectPoolFactory().returnObject(borrowedObj);
		return creator;
	}

	@Override
	public boolean setEntryNameToDatabase(String updateName, StorageI storage) throws Exception {
		Group borrowedObj = ((Group) Group.getObjectPoolFactory()
				.borrowObject());
		boolean result = borrowedObj.setName(this, updateName, storage);
		Group.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public String getTypeFromDatabase(StorageI stoarge) throws Exception {
		return "";
	}

	@Override
	public String getDescriptionFromDatabase(StorageI storage) throws Exception {
		Group borrowedObj = ((Group) Group.getObjectPoolFactory()
				.borrowObject());
		String result = borrowedObj.getDescription(this, storage);
		Group.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public boolean setDescriptionToDatabase(String desc, StorageI storage) throws Exception {
		Group borrowedObj = ((Group) Group.getObjectPoolFactory()
				.borrowObject());
		boolean result = borrowedObj.setDescription(this, desc, storage);
		Group.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public Date getCreationTime(StorageI storage) throws Exception {
		Group borrowedObj = ((Group) Group.getObjectPoolFactory()
				.borrowObject());
		Date result = borrowedObj.getCreationTime(this, storage);
		Group.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public boolean deleteFromDatabase(StorageI storage) throws Exception {
		Group borrowedObj = ((Group) Group.getObjectPoolFactory()
				.borrowObject());
		boolean result = borrowedObj.deleteGroup(this, storage);
		Group.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

}
